<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class upload_service{
	private $CI;
	function __construct() {
		$this->CI = &get_instance();
	}



	//上传图片
	function UploadImg($type,$Folder='',$is_source=''){
		if($is_source != ''){
			$director = "upload/".$Folder."/";
		}else{
			$director = "upload/".$type."/";
		}
	
		$output_dir = $director.date('Ymd',time())."/";
		//pri($director);
		if(!file_exists($output_dir))
		{
			mkdir($output_dir, 0777);
		}
		if(isset($_FILES["myfile"]))
		{
			if(!is_array($_FILES["myfile"]['name'])){
				$error =$_FILES["myfile"]["error"];
				$type=explode('.',$_FILES["myfile"]['name']);
				$document_name=buildOrdeNo();
			 	$fileName = $document_name.'.'.$type[count($type)-1];
			 	//确定icon
			 	$icon='other.png';
			 	foreach ((new CommonDocument)->getAll() as $k => $v) {
			 		if(in_array($type[count($type)-1],$v[XPHEnum::Caption])){
			 			$icon=$v[XPHEnum::Remark];
			 			break;
			 		}
			 	}
			 	if($icon!=CommonDocument::Img[XPHEnum::Remark]){
			 		exit(retJson('上传的文件必须为图片类型：'.implode(',', CommonDocument::Img[XPHEnum::Caption]),false));
			 		//return retJson('上传的文件必须为图片类型：'.implode(',', CommonDocument::Img[XPHEnum::Caption],false));
			 	}
			 	//pri($output_dir.$fileName);
			 	move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
			 	$path=$output_dir.$fileName;


			 	return array('path'=>$path,'icon'=>$icon);
			 	//exit(retJson('',true,$path,$id,$icon));
			}
			else{
				$path=array();
				$icon=array();
				foreach ($_FILES["myfile"]['name'] as $k1 => $v1) {
					$error =$_FILES["myfile"]["error"][$k1];
					$type=explode('.',$_FILES["myfile"]['name'][$k1]);
					$document_name=buildOrdeNo();
				 	$fileName = $document_name.'.'.$type[count($type)-1];
				 	//确定icon
				 	$icon_item='other.png';
				 	foreach ((new CommonDocument)->getAll() as $k => $v) {
				 		if(in_array($type[count($type)-1],$v[XPHEnum::Caption])){
				 			$icon_item=$v[XPHEnum::Remark];
				 			$icon[]=$icon_item;
				 			break;
				 		}
				 	}
				 	if($icon_item!=CommonDocument::Img[XPHEnum::Remark]){
				 		exit(retJson('上传的文件必须为图片类型：'.implode(',', CommonDocument::Img[XPHEnum::Caption]),false));
				 	}
				 	move_uploaded_file($_FILES["myfile"]["tmp_name"][$k1],$output_dir.$fileName);
				 	$path[]=$output_dir.$fileName;
				}
				return array('path'=>$path,'icon'=>$icon);
				//exit(retJson('',true,$path,$id,$icon));
			}
		}else{

			exit(retJson("上传失败",false));
			//return retJson('上传失败',false);
		}


	}


		//文件上传,$type上传文件类型,$is_excel 是否为excel文件
	function  UploadFile($Folder='',$is_excel=false){
        $director = "upload/".$Folder."/";//判断目录是否存在,不存在则创建目录

		if(!file_exists($director))
		{
			mkdir($director, 0777);
		}
		$output_dir = $director.date('Ymd', time())."/";
		if (!file_exists($output_dir))
		{
			mkdir($output_dir, 0777);
		}

		if(isset($_FILES["file"]))
		{
			if(!is_array($_FILES["file"]['name'])){
				$error =$_FILES["file"]["error"];
				$type=explode('.',$_FILES["file"]['name']);
				$document_name=buildOrdeNo();
			 	$fileName = $document_name.'.'.$type[count($type)-1];
			 	//确定icon
			 	$icon='other.png';
			 	foreach ((new CommonDocument)->getAll() as $k => $v) {
			 		if(in_array($type[count($type)-1],$v[XPHEnum::Caption])){
			 			$icon=$v[XPHEnum::Remark];
			 			break;
			 		}
			 	}
			 	if($is_excel == true){
			 		if($icon!=CommonDocument::Excel[XPHEnum::Remark]){
			 			exit(retJson('上传的文件必须为Excel类型：'.implode(',', CommonDocument::Img[XPHEnum::Caption]),false));
			 		}
			 	}
		
			 	move_uploaded_file($_FILES["file"]["tmp_name"],$output_dir.$fileName);
			 	$path=$output_dir.$fileName;
			 //	pri($path);
			 	return array('path'=>$path,'icon'=>$icon);
			 	//exit(retJson('',true,$path,$icon));
			}
			else{
				$path=array();
				$icon=array();
				foreach ($_FILES["file"]['name'] as $k1 => $v1) {
					$error =$_FILES["file"]["error"][$k1];
					$type=explode('.',$_FILES["file"]['name'][$k1]);
					$document_name=buildOrdeNo();
				 	$fileName = $document_name.'.'.$type[count($type)-1];
				 	//确定icon
				 	$icon_item='other.png';
				 	foreach ((new CommonDocument)->getAll() as $k => $v) {
				 		if(in_array($type[count($type)-1],$v[XPHEnum::Caption])){
				 			$icon_item=$v[XPHEnum::Remark];
				 			$icon[]=$icon_item;
				 			break;
				 		}
				 	}
				 	if($is_excel == true){
			 			if($icon_item!=CommonDocument::Excel[XPHEnum::Remark]){
					 		exit(retJson('上传的文件必须为Excel类型：'.implode(',', CommonDocument::Img[XPHEnum::Caption]),false));
					 	}
				 	}
				 	move_uploaded_file($_FILES["file"]["tmp_name"][$k1],$output_dir.$fileName);
				 	$path[]=$output_dir.$fileName;
				}
				return array('path'=>$path,'icon'=>$icon);
			}
		}
		else{
			exit(retJson('上传失败',false));
		}


	}

}